package com.zwj.controller.orderdetail;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mai.cart.Cart;
import com.mai.cart.CartItem;
import com.zwj.entity.ZOrderDetail;
import com.zwj.service.ZOrderDetailService;

@Controller
public class ZOrderDetailController {

	@Autowired
	private ZOrderDetailService zOrderDetailService;
	
	//依据order_id查询订单详情
	@RequestMapping("/selectOrderDetailByid")
	public String selectAllOrderDetailByid(Model model,Integer order_id,HttpServletRequest request) {
		List<ZOrderDetail> lod = this.zOrderDetailService.selectAllOrderDetailByorder_id(order_id);
		model.addAttribute("lod", lod);
		request.getSession().setAttribute("oid", order_id);
		return "detail";
	}
	
	//订单所有订单
	@RequestMapping("/selectAllOrderDetail")
	public String selectAllOrderDetail(Model model) {
		List<ZOrderDetail> lod = this.zOrderDetailService.selectAllOrderDetail();
		model.addAttribute("lod", lod);
		return "houtai";
	}
	
	//删除订单
	@RequestMapping("/deleteOrderDetail")
	public String deleteOrderDetail(Integer order_id) {
		boolean result = this.zOrderDetailService.deleteOrderDetail(order_id);
		if(result) {
			System.out.println("删除成功");
		}else {
			System.out.println("删除失败");
		}
		return "selectAll";
	}
	
	
	//订单合计
	@RequestMapping("/OrderDetail")
	public String selectTotal(Model model) {
		List<ZOrderDetail> lod = this.zOrderDetailService.selectTotal();
		model.addAttribute("lod", lod);
		return "order";
	}
	
	@RequestMapping("/jiesuan")
	public String jiesuan(HttpServletRequest request) {
		ZOrderDetail zd=null;
		int order_id = (int) request.getSession().getAttribute("order_id");
		Cart c =(Cart) request.getSession().getAttribute("cart");
		Map<Integer, CartItem> map = c.getMap();
		for (int key : map.keySet()) {
			int pid = map.get(key).getProduct().getProduct_id();
			String pname = map.get(key).getProduct().getProduct_name();
			double pp = map.get(key).getProduct().getProduct_price();
			int product_quantity =map.get(key).getQuantity();
			zd = new ZOrderDetail(order_id,pid,pname,pp,product_quantity);
			this.zOrderDetailService.addOrderDetail(zd);
		}
		return "index";
	}
}
